/*
// 共六页显示，通过按钮PRG切换，分别：GPS坐标，天通1~3号星方向角仰角，舒拉亚2~3号星方向角仰角
// 使用硬件：heltec公司ESP32-LoRa 和 GPS串口模块
// 硬件连接：GPS模块连接ESP32的GPIO-2，-17脚，用Serial2串口
// 使用库 heltec公司 
//  heltec.h: Heltec ESP32 Dev-Boards 1.1.0版
//  TinyGPSPlus: TinyGPSOlus-ESP32 0.0.2版
//  Sgp4: SparkFun Electronics 1.0.3版
//  TimeLib: Paul Stoffregen 1.6.1版
//  还存在的问题：按钮切换要等待GPS时间正确，否则卫星方向角仰角数据不准确，但时间对之后，数据也就对了
// 开发环境 arduino-1.8.15-windows，加载
// https://github.com/Heltec-Aaron-Lee/WiFi_Kit_series/releases/download/0.0.5/package_heltec_esp32_index.json
// 开发版选择HelTec V2
//   2022.4.4 Yang
*/

#include <TinyGPSPlus.h>
#include <heltec.h>
#include <Sgp4.h>
#include <TimeLib.h>
#define USER_SERIAL Serial
#define GPS_SERIAL  Serial2
Sgp4 TT1sat, TT2sat, TT3sat,TH2sat, TH3sat;
TinyGPSPlus gps;
//当前位置的经度Lat，纬度Lon，高度Alt从GPS获得，赋值给sgp4数据结构
double siteLatTmp = 31.169550, siteLonTmp = 121.447655, siteAltTmp = 1.0;
//当前的时间 UNIX格式时间，2022.4.3 12:00左右=1648959770, te是可阅读时间模式
tmElements_t te;
unsigned long Me_unixtime = 1648959770;
//当前时区 utc + 8 中国时区
int Me_timezone = 8 ;
//GPS信号NMEA协议的UTC时间$GNGGA,013520.200,3110.17150,N,12126.85900,E,1,12,1.1,30.8,M,0.0,M,,*4E
//013520.200 存于 gps.time.hour minute second中
//数据结构，用于翻页的按钮
struct btnPage {
    const uint8_t PIN;
    int num;
    bool pressed;
};
btnPage btnPage0 = {0, 1, false};//指定PIN脚=0 GPIO-0，次数从1开始，到6结束

void IRAM_ATTR isr(void* arg) {
    btnPage* s = static_cast<btnPage*>(arg);
    s->num += 1;
    if ( s->num > 6){s->num = 1;}
    s->pressed = true;
}

//天通卫星-1的sgp4数据结构
char TT1satname[] = "TIANTONG-1 1";
char TT1tle_line1[] = "1 41725U 16048A   22092.82164412 -.00000329  00000-0  00000-0 0  9996";
char TT1tle_line2[] = "2 41725   2.3128 354.8244 0004228  31.8630 201.6554  1.00271752 20868";
char TT2satname[] = "TIANTONG-1 2";
char TT2tle_line1[] = "1 46916U 20082A   22092.82635390 -.00000370  00000-0  00000-0 0  9990";
char TT2tle_line2[] = "2 46916   4.3102 296.0263 0003257 114.7718 202.7327  1.00276474  5247";
char TT3satname[] = "TIANTONG-1 3";
char TT3tle_line1[] = "1 47321U 21003A   22092.67413910 -.00000169  00000-0  00000-0 0  9996";
char TT3tle_line2[] = "2 47321   4.5951 308.3500 0003892  56.7329 150.0830  1.00270589  4560";
//舒拉亚卫星02的sgp4数据结构
char TH2satname[] = "THURAYA 2";
char TH2tle_line1[] = "1 27825U 03026A   22092.81021535  .00000121  00000-0  00000-0 0  9999";
char TH2tle_line2[] = "2 27825   6.6423  34.3628 0002790 320.4242 172.0010  1.00272539 68945";
//舒拉亚卫星03的sgp4数据结构
char TH3satname[] = "THURAYA 3";
char TH3tle_line1[] = "1 32404U 08001A   22092.82103359 -.00000310  00000-0  00000-0 0  9993";
char TH3tle_line2[] = "2 32404   4.0081  21.9006 0002169 355.4262 207.8728  1.00272678 52083";
/* 2022.4.3 www.openats.cn/tle/catalog.txt www.chnsatcom.com/tle/catalog.txt
0 TIANTONG-1 1 天通卫星01
1 41725U 16048A   22092.82164412 -.00000329  00000-0  00000-0 0  9996
2 41725   2.3128 354.8244 0004228  31.8630 201.6554  1.00271752 20868
0 TIANTONG-1 2 天通卫星02
1 46916U 20082A   22092.82635390 -.00000370  00000-0  00000-0 0  9990
2 46916   4.3102 296.0263 0003257 114.7718 202.7327  1.00276474  5247
0 TIANTONG-1 3 天通卫星03
1 47321U 21003A   22092.67413910 -.00000169  00000-0  00000-0 0  9996
2 47321   4.5951 308.3500 0003892  56.7329 150.0830  1.00270589  4560
0 THURAYA 2 舒拉亚卫星02
1 27825U 03026A   22092.81021535  .00000121  00000-0  00000-0 0  9999
2 27825   6.6423  34.3628 0002790 320.4242 172.0010  1.00272539 68945
0 THURAYA 3 舒拉亚卫星03
1 32404U 08001A   22092.82103359 -.00000310  00000-0  00000-0 0  9993
2 32404   4.0081  21.9006 0002169 355.4262 207.8728  1.00272678 52083
*/
void setup() {
  //begin函数（显示允许，LaRa射频禁止，串口允许）
  Heltec.begin(true, false, true);
  Heltec.display->init();
  Heltec.display->flipScreenVertically();  
  Heltec.display->clear();
  Heltec.display->setFont(ArialMT_Plain_16);
  Heltec.display->setTextAlignment(TEXT_ALIGN_CENTER);
  Heltec.display->drawString(64, 16, "Waiting GPS");
  USER_SERIAL.println("Waiting GPS");
  //Serial2自定义串口GPS_SERIAL，rx=2脚对应GPS的TX & tx=17脚对应GPS的RX 
  GPS_SERIAL.begin(9600, SERIAL_8N1, 2, 17);
  //while (!GPS_SERIAL) ; // 等待GPS串口连接
  delay(500);
  Heltec.display->display();
  //初始化sat卫星参数
  TT1sat.init(TT1satname,TT1tle_line1,TT1tle_line2);
  TT2sat.init(TT2satname,TT2tle_line1,TT2tle_line2);
  TT3sat.init(TT3satname,TT3tle_line1,TT3tle_line2);
  TH2sat.init(TH2satname,TH2tle_line1,TH2tle_line2);
  TH3sat.init(TH3satname,TH3tle_line1,TH3tle_line2);
  pinMode(btnPage0.PIN, INPUT_PULLUP);
  attachInterruptArg(btnPage0.PIN, isr, &btnPage0, FALLING);

}

void loop() {
  String str = "";
  char buff[12];
  char c;
  if (btnPage0.pressed) {
     //Serial.printf("Button 1 has been pressed %u times\n", btnPage0.num);
     btnPage0.pressed = false;
  }
  //串口1有效，则接收GPS内容
  while (GPS_SERIAL.available() > 0)
  {
    if (gps.encode(GPS_SERIAL.read()))
    {
      if( gps.date.isValid() && gps.time.isUpdated()) 
      {
        Heltec.display->clear();
        if (btnPage0.num == 1)
        {

        if (gps.location.isValid() && gps.location.isUpdated())
        {
        siteLatTmp = gps.location.lat();
        siteLonTmp = gps.location.lng();
        Heltec.display->setFont(ArialMT_Plain_24);
        Heltec.display->setTextAlignment(TEXT_ALIGN_CENTER);
        Heltec.display->drawString(64, 16, String(siteLatTmp, 6));
        Heltec.display->drawString(64, 40, String(siteLonTmp, 6));
        TT1sat.site(siteLatTmp, siteLonTmp, siteAltTmp);
        TT2sat.site(siteLatTmp, siteLonTmp, siteAltTmp);
        TT3sat.site(siteLatTmp, siteLonTmp, siteAltTmp);
        TH2sat.site(siteLatTmp, siteLonTmp, siteAltTmp);
        TH3sat.site(siteLatTmp, siteLonTmp, siteAltTmp);
        }
        Heltec.display->setFont(ArialMT_Plain_16);
        Heltec.display->setTextAlignment(TEXT_ALIGN_LEFT);
        str = "UTC=";
        sprintf(buff, "%02d", gps.time.hour());
        str = str + buff +":";
        sprintf(buff, "%02d", gps.time.minute());
        str = str + buff +":";
        sprintf(buff, "%02d", gps.time.second()); 
        str = str + buff +" ";
        sprintf(buff, "%02d", gps.satellites.value()); 
        str = str + buff;
        USER_SERIAL.println(str);
        Heltec.display->drawString(0, 0, str);
        str = "";
        uint16_t aYear = gps.date.year() - 1970;
        te.Year = uint8_t(aYear);
        te.Month = gps.date.month();
        te.Day = gps.date.day();
        te.Hour = gps.time.hour();
        te.Minute = gps.time.minute();
        te.Second = gps.time.second();
        Me_unixtime =  makeTime(te);
        //通过Me_unixtime计算方位角、仰角
        TT1sat.findsat(Me_unixtime);
        TT2sat.findsat(Me_unixtime);
        TT3sat.findsat(Me_unixtime);
        TH2sat.findsat(Me_unixtime);
        TH3sat.findsat(Me_unixtime);

        } 
        if(btnPage0.num == 2)
        {
        Heltec.display->setFont(ArialMT_Plain_16);
        Heltec.display->setTextAlignment(TEXT_ALIGN_LEFT);
        Heltec.display->drawString(0, 0, "TianTong1-01");
        Heltec.display->drawString(0, 20, "direction  " + String(TT1sat.satAz));
        Heltec.display->drawString(0, 40, "Elevation  " + String(TT1sat.satEl));
        USER_SERIAL.println("天通1 01方位角 = " + String( TT1sat.satAz) + " 仰角 = " + String(TT1sat.satEl));
        } 
        if(btnPage0.num == 3)
        {
        Heltec.display->setFont(ArialMT_Plain_16);
        Heltec.display->setTextAlignment(TEXT_ALIGN_LEFT);
        Heltec.display->drawString(0, 0, "TianTong1-02");
        Heltec.display->drawString(0, 20, "direction  " + String(TT2sat.satAz));
        Heltec.display->drawString(0, 40, "Elevation  " + String(TT2sat.satEl));
        USER_SERIAL.println("天通1 02方位角 = " + String( TT2sat.satAz) + " 仰角 = " + String(TT2sat.satEl));
        } 
        if(btnPage0.num == 4)
        {
        Heltec.display->setFont(ArialMT_Plain_16);
        Heltec.display->setTextAlignment(TEXT_ALIGN_LEFT);
        Heltec.display->drawString(0, 0, "TianTong1-03");
        Heltec.display->drawString(0, 20, "direction  " + String(TT3sat.satAz));
        Heltec.display->drawString(0, 40, "Elevation  " + String(TT3sat.satEl));
        USER_SERIAL.println("天通1 03方位角 = " + String( TT3sat.satAz) + " 仰角 = " + String(TT3sat.satEl));
        } 
        if(btnPage0.num == 5)
        {
        Heltec.display->setFont(ArialMT_Plain_16);
        Heltec.display->setTextAlignment(TEXT_ALIGN_LEFT);
        Heltec.display->drawString(0, 0, "THURAYA 2");
        Heltec.display->drawString(0, 20, "direction  " + String(TH2sat.satAz));
        Heltec.display->drawString(0, 40, "Elevation  " + String(TH2sat.satEl));
        USER_SERIAL.println("舒拉亚2 方位角 = " + String( TH2sat.satAz) + " 仰角 = " + String(TH2sat.satEl));
        } 
        if(btnPage0.num == 6)
        {
        Heltec.display->setFont(ArialMT_Plain_16);
        Heltec.display->setTextAlignment(TEXT_ALIGN_LEFT);
        Heltec.display->drawString(0, 0, "THURAYA 3");
        Heltec.display->drawString(0, 20, "direction  " + String(TH3sat.satAz));
        Heltec.display->drawString(0, 40, "Elevation  " + String(TH3sat.satEl));
        USER_SERIAL.println("舒拉亚3 方位角 = " + String( TH3sat.satAz) + " 仰角 = " + String(TH3sat.satEl));
        }
        }
      Heltec.display->display();
      delay(500);
    }
  }
}
